<template>
  <div>

<!--    <div>-->
      <el-select v-model="value" placeholder="请选择要搜索的字段" size="mini">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <!--  搜索-->
      <el-input v-model="search" placeholder="请输入要查询的字段的值" style="width: 20%;margin-left: 10px" size="mini"></el-input>
      <el-button type="primary" style="margin-top: 10px;margin-left: 10px" @click="ShowSearch()" icon="el-icon-search" size="mini">查询</el-button>
<!--    </div>-->
    <div style="margin-top: 10px;margin-bottom: 10px">
      <el-button
          size="mini"
          type="primary"
          class="handle-del mr10"
          @click="ShowAllET()"
      >展示所有数据</el-button>
      <el-button
          size="mini"
          type="primary"
          class="handle-del mr10"
          @click="SchoolETAdd()"
          icon="el-icon-circle-plus-outline"
      >添加评估任务</el-button>
<!--      <el-button-->
<!--          type="primary"-->
<!--          class="handle-del mr10"-->
<!--          @click="Import()"-->
<!--      >导入</el-button>-->
      <el-button
          size="mini"
          type="primary"
          class="handle-del mr10"
          @click="Export()"
          icon="el-icon-download"
      >导出</el-button>
    </div>
    <el-table
        size="mini"
        :data=this.tableData
        border
        :row-style="{height:'10px'}"
        :cell-style="{padding:'0px'}"
        :header-cell-style="{background:'lightblue',color:'#262626'}"
        style="width: 1086px">
      <el-table-column
          prop="evaluatetaskid"
          label="评估任务号"
          align="center"
          width="95">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskname"
          label="评估任务名称"
          align="center"
          width="150">
      </el-table-column>
      <el-table-column
          prop="evaluateid"
          label="评估号"
          align="center"
          width="80">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskopentime"
          label="评估任务开始时间"
          align="center"
          width="180">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskendtime"
          label="评估任务结束时间"
          align="center"
          width="180">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskcontent"
          label="评估任务内容"
          align="center"
          width="300">
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
          width="100">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row),dialogFormVisible = true" type="text" size="small">修改</el-button>
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table
        :data=this.tableData1
        border
        hidden
        id="out-table1"
        :header-cell-style="{background:'lightblue',color:'#262626'}"
        style="width: 1086px">
      <el-table-column
          prop="evaluatetaskid"
          label="评估任务号"
          width="95">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskname"
          label="评估任务名称"
          width="150">
      </el-table-column>
      <el-table-column
          prop="evaluateid"
          label="评估号"
          width="80">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskopentime"
          label="评估任务开始时间"
          width="180">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskendtime"
          label="评估任务结束时间"
          width="180">
      </el-table-column>
      <el-table-column
          prop="evaluatetaskcontent"
          label="评估任务内容"
          width="300">
      </el-table-column>
    </el-table>
    <el-dialog :title=this.modeName :visible.sync="dialogFormVisible" :show-close=false>
      <hr style="margin-top: -30px;margin-bottom: 10px">
      <el-form :model="ruleForm" :rules="rules" :inline="true" ref="ruleForm" label-width="180px" class="demo-ruleForm">
        <el-form-item label="评估任务号" prop="evaluatetaskid">
          <el-input v-model="ruleForm.evaluatetaskid"></el-input>
        </el-form-item>
        <el-form-item label="评估任务名称" prop="evaluatetaskname">
          <el-input v-model="ruleForm.evaluatetaskname"></el-input>
        </el-form-item>
        <el-form-item label="评估号" prop="evaluateid">
          <el-input v-model="ruleForm.evaluateid"></el-input>
        </el-form-item>
        <el-form-item label="评估任务开始时间" prop="evaluatetaskopentime">
          <el-date-picker
              v-model="ruleForm.evaluatetaskopentime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评估任务结束时间" prop="evaluatetaskendtime">
          <el-date-picker
              v-model="ruleForm.evaluatetaskendtime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评估任务内容" prop="evaluatetaskcontent">
          <el-input v-model="ruleForm.evaluatetaskcontent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm'),allid=''">确定更新</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="dialogFormVisible = false,allid=''">取 消</el-button>
        <!--        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>-->
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  methods:{
    submitForm(formName) {
      const _this=this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.allid){
            axios.put("http://localhost:8081/evaluatetask/update",this.ruleForm).then(function (res){
              if (res.data.msg==="更新成功"){
                _this.$message({
                  message: '更新成功！',
                  type: 'success'
                  //添加成功后页面跳转
                });
                // _this.$router.push('/EvaluateTaskList')
                _this.dialogFormVisible=false
                // _this.$router.push('/SchoolHost')
                axios.get('http://localhost:8081/evaluatetask/findAll').then(function (res){
                  let etlen=res.data.length
                  while (etlen){
                    etlen--
                    res.data[etlen].evaluatetaskopentime=_this.$moment(res.data[etlen].evaluatetaskopentime).format('YYYY-MM-DD HH:mm:ss')
                    res.data[etlen].evaluatetaskendtime=_this.$moment(res.data[etlen].evaluatetaskendtime).format('YYYY-MM-DD HH:mm:ss')
                  }
                  _this.tableData=res.data
                })
              }
              else{
                _this.$message({
                  message: '评估任务号重复或者评估号不存在！请重新输入',
                  type: 'fail'
                });
              }
            })
          }
          else {
            axios.post("http://localhost:8081/evaluatetask/save",this.ruleForm).then(function (res){
              if (res.data.msg==="添加成功"){
                _this.$message({
                  message: '添加成功！',
                  type: 'success'
                  //添加成功后页面跳转
                });
                // _this.$router.push('/EvaluateTaskList')
                _this.dialogFormVisible=false
                axios.get('http://localhost:8081/evaluatetask/findAll').then(function (res){
                  let etlen=res.data.length
                  while (etlen){
                    etlen--
                    res.data[etlen].evaluatetaskopentime=_this.$moment(res.data[etlen].evaluatetaskopentime).format('YYYY-MM-DD HH:mm:ss')
                    res.data[etlen].evaluatetaskendtime=_this.$moment(res.data[etlen].evaluatetaskendtime).format('YYYY-MM-DD HH:mm:ss')
                  }
                  _this.tableData=res.data
                })
              }
              else{
                _this.$message({
                  message: '评估任务号重复或评估号不存在！请重新输入',
                  type: 'fail'
                });
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    ShowAllET(){
      const _this=this
      axios.get('http://localhost:8081/evaluatetask/findAll').then(function (res){
        _this.tableData=res.data
      })
    },

    ShowSearch(){
      const _this=this
      axios.get("http://localhost:8081/evaluatetask/search/"+this.value+'/'+this.search).then(function (res){
        _this.tableData=res.data
        _this.search=''
      })
    },

    // edit(row){
    //   console.log(row.id)
    //   //将当前行对象传过去
    //   this.$router.push({
    //     path:'/UpdateEvaluateTask',
    //     //query带参数传递
    //     query:{
    //       id:row.id
    //     }
    //   })
    // },
    Export(){
      let tables = document.getElementById("out-table1");
      console.log(tables)
      let table_book = this.$XLSX.utils.table_to_book(tables);
      var table_write = this.$XLSX.write(table_book, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        this.$FileSaver.saveAs(
            new Blob([table_write], { type: "application/octet-stream" }),
            "EvaluateTask.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, table_write);
      }
      return table_write;
    },
    SchoolETAdd(){
      this.dialogFormVisible = true;
      this.modeName='添加评估任务'
      this.ruleForm={}
    },
    edit(row){
      this.dialogFormVisible = true
      this.modeName='更新评估任务'
      this.ruleForm=row
      this.allid=row.id
    },
    del(row){
      const d=confirm("确定删除吗？");
      if (d){
        axios.delete('http://localhost:8081/evaluatetask/delete/'+row.evaluatetaskid).then(()=>{
          alert("删除成功")
          this.ShowAllET()
        })
      }

    }

  },

  data(){
    return {
      search:'',
      allid:'',
      mode:'',
      modeName:'',
      options: [{
        value: '1',
        label: '评估任务号'
      }, {
        value: '2',
        label: '评估任务名称'
      },
        {
          value: '3',
          label: '评估名称'
        },
        {
          value: '4',
          label: '评估任务开始时间'
        },
        {
          value: '5',
          label: '评估任务结束时间'
        },
        {
          value: '6',
          label: '评估任务内容'
        },
        ],
      value: '',
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      total:null,
      tableData: null,
      tableData1: null,
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      ruleForm: {
        // id:'',
        evaluatetaskid: '',
        evaluatetaskname: '',
        evaluateid:'',
        evaluatetaskopentime:'',
        evaluatetaskendtime:'',
        evaluatetaskcontent:''
      },
      rules: {
        id: [
          { required: true, message: '请填写编号 注意与已存在的编号不要重复', trigger: 'blur' }
        ],
        evaluatetaskid: [
          { required: true, message: '请填写评估任务号', trigger: 'blur' }
        ],
        evaluatetaskname: [
          { required: true, message: '请填写评估任务名称', trigger: 'blur' }
        ],
        evaluateid: [
          { required: true, message: '请填写评估号', trigger: 'blur' }
        ],
        evaluatetaskopentime: [
          { required: true, message: '请填写评估任务开始时间', trigger: 'blur' }
        ],
        evaluatetaskendtime: [
          { required: true, message: '请填写评估任务结束时间', trigger: 'blur' }
        ],
        evaluatetaskcontent: [
          { required: true, message: '请填写评估任务内容', trigger: 'blur' }
        ],
      },
      dataList: [],
      fileList: [],
      query: {"page":0, "size":20, count:0},
      column: [
        {label: "序号", prop: "id", type: "string"},
        {label: "评估任务号", prop: "evaluatetaskid", type: "string"},
        {label: "评估任务名称", prop: "evaluatetaskname", type: "string"},
        {label: "评估号", prop: "evaluateid", type: "string"},
        {label: "评估任务开始时间", prop: "evaluatetaskopentime", type: "string"},
        {label: "评估任务结束时间", prop: "evaluatetaskendtime", type: "string"},
        {label: "评估任务内容", prop: "evaluatetaskcontent", type: "string"},

      ],
      editForm: {},
      editVisible: false,
      currentIndex: null,
    }
  },
  created() {
      const _this=this
      const a=localStorage.getItem("access-admin")
      if(a){
        // this.admin=JSON.parse(window.localStorage.getItem('access-admin'))
        axios.get('http://localhost:8081/evaluatetask/findAll').then(function (res){
          let etlen=res.data.length
          while (etlen){
            etlen--
            res.data[etlen].evaluatetaskopentime=_this.$moment(res.data[etlen].evaluatetaskopentime).format('YYYY-MM-DD HH:mm:ss')
            res.data[etlen].evaluatetaskendtime=_this.$moment(res.data[etlen].evaluatetaskendtime).format('YYYY-MM-DD HH:mm:ss')
          }
          _this.tableData=res.data
          _this.tableData1=res.data
        })
      }
      else{
        this.$alert('未登录','提示',{
          confirmButtonText:'确定'
        }).then((res)=>{
          this.$router.replace({path:'/login'})
        })
      }
  }
}
</script>
